package com.ywq.app.web.mvc.misc;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ywq.app.biz.AdminBiz;
import com.ywq.app.biz.ArtMadeBiz;
import com.ywq.app.biz.ArtRoomBiz;
import com.ywq.app.biz.AuxContentBiz;
import com.ywq.app.domain.AdminUser;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtMade;
import com.ywq.app.domain.ArtMadeAudit;
import com.ywq.app.domain.ArtMadeTalk;
import com.ywq.app.domain.ArtRoom;
import com.ywq.app.domain.ArtRoomPic;
import com.ywq.app.domain.Content;
import com.ywq.app.domain.Member;
import com.ywq.app.domain.OnLineUserListener;
import com.ywq.app.web.mvc.artRoom.ArtRoomController;
import com.ywq.app.web.util.AdminUtil;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.Encode;

@Controller
public class ArtMadeMiscController {
	private final static Logger logger = LoggerFactory.getLogger(ArtRoomController.class);
	@Autowired
	private ArtMadeBiz artMadeBiz;
	@Autowired
	private AuxContentBiz auxContentBiz;
	//
	/**
	 * 展览馆列表页面
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artMade/artistList.htm")
	public String artRoom(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
			return "misc/artMade/artMade";
			}
	//查询所有属于定制的艺术家
	@RequestMapping(value = "/misc/artMade/artMadeGrid.htm")
	public String artMadeGrid(Integer page,Integer pagesize, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
	
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			PageModel pageModel = artMadeBiz.getArtistListForMisc(page,pagesize);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModel));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtistList failure.", e);
			}
		}
		return null;
	}
	//添加艺术家页面
	@RequestMapping(value = "/misc/artMade/addArtistWeb.htm")
	public String addArtistWeb(HttpServletRequest request, HttpServletResponse response) throws IOException {
			request.setAttribute("have", "no");
			return "misc/artMade/addArtMadeWeb";
			}
	//修改艺术家
	@RequestMapping(value = "/misc/artMade/updateArtistWeb.htm")
	public String upadteArtistWeb(String artistId,HttpServletRequest request, HttpServletResponse response) throws IOException {
			 try {
				ArtArtist artArtist =artMadeBiz.getArtist(artistId);
				request.setAttribute("have", "have");
				request.setAttribute("artArtist", artArtist);
				} catch (AppBusinessException e) {
					e.printStackTrace();
				}
			return "misc/artMade/addArtMadeWeb";
			}
	//修改艺术家
	
	@RequestMapping(value = "/misc/artMade/updateArtist.htm", method = RequestMethod.POST)
	public String updateArtist(HttpServletRequest request, HttpServletResponse response, ArtArtist artArtist) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artMadeBiz.updateArtist(artArtist);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("insertArtist failure.", e);
			}
		}
		return null;
		
	}
	
	//添加艺术家
	@RequestMapping(value = "/misc/artMade/insertArtist.htm", method = RequestMethod.POST)
	public String insertArtist(HttpServletRequest request, HttpServletResponse response, ArtArtist artArtist) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			//添加艺术家 //同时生成一个登陆账号
			artMadeBiz.insertArtArtist(artArtist);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("insertArtist failure.", e);
			}
		}
		return null;
		
	}
	//查看某一艺术家作品页面
	@RequestMapping(value = "/misc/made/showPicList.htm")
	public String showPicList(String artistId, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
			request.setAttribute("artistId", artistId);
		return "misc/artMade/showArtistPic";
	}
	//查看某一艺术家作品列表
	@RequestMapping(value = "/misc/index/artistPicListGrid.htm")
	public String artistPicListGrid(String artistId,Integer page,Integer pagesize, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			PageModel pageModel = artMadeBiz.getArtistPicListForMisc(page,artistId,pagesize);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModel));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtistList failure.", e);
			}
		}
		return null;
	}
	//添加内容页面
	@RequestMapping(value = "/misc/made/addContentGs.htm")
	public String addContentGs(String artistId,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
			request.setAttribute("channel", artistId);
			try {
				Content story =auxContentBiz.getStoryByArtistId(artistId);
				if(story!=null){
					request.setAttribute("story", story);
					request.setAttribute("have", "have");
				}else if(story==null){
					request.setAttribute("have", "no");
				}
			} catch (AppBusinessException e) {
				e.printStackTrace();
			}
			request.setAttribute("type", "gs");
		return "misc/artMade/addContentStory";
	}
	@RequestMapping(value = "/misc/artMade/madeOrdersList.htm")
	public String madeOrdersList(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
			return "misc/artMade/madeOrderList";
			}
	
	//定制订单
	@RequestMapping(value = "/misc/artMade/madeOrderGrid.htm")
	public String madeOrderGrid(Integer page,Integer pagesize, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		AdminUser user =AdminUtil.getUser();
		if(user!=null){
			try {
				if (page == null) {
					page = 1;
				}
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("page", page);
				map.put("pagesize", pagesize);
				if(!"".equals(user.getArtistId()) && user.getArtistId()!=null){
					map.put("artistId", user.getArtistId());
				}
				PageModel pageModel = artMadeBiz.getMadeOrderList(map);
				pw = response.getWriter();
				pw.print(JSONObject.fromObject(pageModel));
			} catch (Exception e) {
				pw = response.getWriter();
				pw.print("error");
				if (logger.isErrorEnabled()) {
					logger.error("madeOrderGrid failure.", e);
				}
			}
		}
		return null;
	}
	//定制详情
	@RequestMapping(value = "/misc/made/showTalkListByMadeId.htm")
	public String showTalkListByMadeId(String madeId,HttpServletRequest request, HttpServletResponse response) throws IOException {
			request.setAttribute("madeId", madeId);
			return "misc/artMade/showTalkList";
		}
	//定制对话列表
	@RequestMapping(value = "/misc/artMade/talkGrid.htm")
	public String talkGrid(Integer page,Integer pagesize, String madeId,HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		AdminUser user =AdminUtil.getUser();
		if(user!=null){
			try {
				if (page == null) {
					page = 1;
				}
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("page", page);
				map.put("pagesize", pagesize);
				map.put("madeId", madeId);
				PageModel pageModel = artMadeBiz.getTalkGrid(map);
				//List<ArtMadeTalk> talkList =artMadeBiz.getTalkListByMadeId(id);
				pw = response.getWriter();
				pw.print(JSONObject.fromObject(pageModel));
			} catch (Exception e) {
				pw = response.getWriter();
				pw.print("error");
				if (logger.isErrorEnabled()) {
					logger.error("madeOrderGrid failure.", e);
				}
			}
		}
		return null;
	}
	//回复
	@RequestMapping(value = "/misc/made/answerTalkByMadeId.htm")
	public String answerTalkByMadeId(String madeId,HttpServletRequest request, HttpServletResponse response) throws IOException {
			request.setAttribute("madeId", madeId);
			return "misc/artMade/answerTalk";
		}
	//保存回复
	@RequestMapping(value = "/misc/made/addTalkMisc.htm")
	public void addArtMadeTalk(ArtMadeTalk artMadeTalk,HttpServletRequest request,HttpServletResponse response) throws AppBusinessException {
		response.setContentType("text/html; charset=UTF-8");
		
		try {
			PrintWriter out = response.getWriter(); 
			AdminUser user =AdminUtil.getUser();
				if (user!=null){
					artMadeTalk.setMemberId(user.getArtistId());
					ArtMade artMade=artMadeBiz.getArtMadeById(artMadeTalk.getMadeId());
					artMadeTalk.setMadeStage(artMade.getStage());
					artMadeBiz.insertArtMadeTalk(artMadeTalk);
				out.print("success");
				} else {
					out.print("error");
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//提交审核
	@RequestMapping(value = "/misc/made/auditAdmin.htm")
	public String auditAdmin(String madeId,HttpServletRequest request, HttpServletResponse response) throws IOException {
			try {
				ArtMade artMade=artMadeBiz.getArtMadeById(madeId);
				request.setAttribute("artMade", artMade);
				request.setAttribute("madeId", artMade.getIdentifier());
				//查询提交的所有价格auditKey
				 ArtMadeAudit artMadeAuditFirst =artMadeBiz.getArtMadeAuditByMadeId(madeId,"01");
				 ArtMadeAudit artMadeAuditSecond =artMadeBiz.getArtMadeAuditByMadeId(madeId,"02");
				 ArtMadeAudit artMadeAuditThird =artMadeBiz.getArtMadeAuditByMadeId(madeId,"03");
				if(artMadeAuditFirst!=null){
					request.setAttribute("firstMoney", artMadeAuditFirst.getFirstMoney());
					request.setAttribute("secondMoney", artMadeAuditFirst.getFirstMoney());
					request.setAttribute("thirdMoney", artMadeAuditFirst.getFirstMoney());
					request.setAttribute("totalMoney", artMadeAuditFirst.getTotalMoney());
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					
					try {
						Date start = sdf.parse(artMadeAuditFirst.getStartDate());
						Date end = sdf.parse(artMadeAuditFirst.getEndDate());
						request.setAttribute("startDate", start);
						request.setAttribute("endDate", end);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if(artMadeAuditSecond!=null){
					request.setAttribute("secondMoney", artMadeAuditSecond.getSecondMoney());
					request.setAttribute("thirdMoney", artMadeAuditSecond.getThirdMoney());
					request.setAttribute("totalMoney", artMadeAuditSecond.getTotalMoney());
				}
				if(artMadeAuditThird!=null){
					request.setAttribute("thirdMoney", artMadeAuditThird.getThirdMoney());
					request.setAttribute("totalMoney", artMadeAuditThird.getTotalMoney());
				}
			} catch (AppBusinessException e) {
				e.printStackTrace();
			}
			return "misc/artMade/auditTalk";
		}
	//提交管理员审核
	@RequestMapping(value = "/misc/made/saveMadeAudit.htm")
	public void saveMadeAudit(ArtMadeAudit artMadeAudit,HttpServletRequest request, HttpServletResponse response) throws IOException {
			response.setContentType("text/html; charset=UTF-8");
		
		try {
			PrintWriter out = response.getWriter(); 
			AdminUser user =AdminUtil.getUser();
				if (user!=null){
					ArtMade artMade=artMadeBiz.getArtMadeById(artMadeAudit.getMadeId());
					artMadeAudit.setArtistId(user.getArtistId());
					artMadeAudit.setAuditKey(artMade.getStage());
					artMadeBiz.insertMadeAudit(artMadeAudit);
				out.print("success");
				} else {
					out.print("error");
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
	//查看所有审查
	@RequestMapping(value = "/misc/artMade/madeAuditList.htm")
	public String madeAuditList(HttpServletRequest request, HttpServletResponse response) throws IOException {
			return "misc/artMade/madeAuditList";
		}
	//审核列表
	@RequestMapping(value = "/misc/artMade/madeAuditListGrid.htm")
	public String madeAuditListGrid(Integer page,Integer pagesize, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		AdminUser user =AdminUtil.getUser();
		if(user!=null){
			try {
				if (page == null) {
					page = 1;
				}
				PageModel pageModel = artMadeBiz.getArtMadeAuditGrid(page,pagesize);
				pw = response.getWriter();
				pw.print(JSONObject.fromObject(pageModel));
			} catch (Exception e) {
				pw = response.getWriter();
				pw.print("error");
				if (logger.isErrorEnabled()) {
					logger.error("madeAuditListGrid failure.", e);
				}
			}
		}
		return null;
	}
	//通过
	@RequestMapping(value = "/misc/made/passMadeAudit.htm")
	public void passMadeAudit(String auditId,HttpServletRequest request, HttpServletResponse response) throws IOException {
			response.setContentType("text/html; charset=UTF-8");
		
		try {
			PrintWriter out = response.getWriter(); 
			AdminUser user =AdminUtil.getUser();
				if (user!=null){
					artMadeBiz.passMadeAudit(auditId);
				out.print("success");
				} else {
					out.print("error");
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
	//艺术家审核列表
	@RequestMapping(value = "/misc/artMade/queryAuditListByArtistId.htm")
	public String queryAuditListByArtistId(HttpServletRequest request, HttpServletResponse response) throws IOException {
			return "misc/artMade/queryArtistAuditList";
		}
	@RequestMapping(value = "/misc/artMade/queryArtistAuditListGrid.htm")
	public String queryArtistAuditListGrid(Integer page,Integer pagesize, HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		AdminUser user =AdminUtil.getUser();
		if(user!=null){
			try {
				if (page == null) {
					page = 1;
				}
				PageModel pageModel = artMadeBiz.queryArtistAuditListGrid(user.getArtistId(),page,pagesize);
				pw = response.getWriter();
				pw.print(JSONObject.fromObject(pageModel));
			} catch (Exception e) {
				pw = response.getWriter();
				pw.print("error");
				if (logger.isErrorEnabled()) {
					logger.error("madeAuditListGrid failure.", e);
				}
			}
		}
		return null;
	}
	//是否可以提交
	@RequestMapping(value = "/misc/made/isCanAudit.htm")
	public void isCanAudit(String madeId,HttpServletRequest request, HttpServletResponse response) throws IOException {
			response.setContentType("text/html; charset=UTF-8");
		
		try {
			PrintWriter out = response.getWriter(); 
			AdminUser user =AdminUtil.getUser();
				if (user!=null){
					ArtMade artMade=artMadeBiz.getArtMadeById(madeId);
					if(artMade!=null){
						//if(("01").equals(artMade.getStage())){
							ArtMadeAudit artMadeAudit=artMadeBiz.getArtMadeAuditByMadeId(madeId, artMade.getStage());
							if(artMadeAudit==null){
								out.print("true");
							}else{
								out.print("false");
							}
						//}else if(("02").equals(artMade.getStage())){
							
						//}else if(("03").equals(artMade.getStage())){
						//}
					}
					//artMadeBiz.passMadeAudit(auditId);
				
				} else {
					out.print("error");
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
	@RequestMapping(value = "/misc/made/showMadeOrder.htm")
	public String showMadeOrder(String madeId, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
			try {
				ArtMade artMade =artMadeBiz.getArtMadeById(madeId);
				request.setAttribute("artMade", artMade);
			} catch (AppBusinessException e) {
				e.printStackTrace();
			}
		return "misc/artMade/showMadeOrder";
	}
}
